package com.example.healing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.healing.entity.User;
import com.example.healing.mapper.UserMapper;
import com.example.healing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    UserMapper userMapper;
    @Override
    public int create(User user) {
        user.setUid(UUID.randomUUID().toString().replace("-",""));
        return userMapper.insert(user);
    }

    @Override
    public User login(User user) {
        User u = userMapper.selectOne(new QueryWrapper<User>()
                .eq("loginname",user.getLoginname())
                .eq("password",user.getPassword()));
        if (null == u) {
            return null;
        }
        u.setPassword(null);
        return u;
    }

    @Override
    public int edit(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public User getUser(String loginname) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("longinname", loginname));
    }

    @Override
    public User getUserById(String id) {
        return userMapper.selectOne(new QueryWrapper<User>().eq("uid", id));
    }
}
